package sse.ustc.cc.adapter;

import java.util.List;
import sse.ustc.cc.activity.R;
import sse.ustc.cc.util.WXMessage;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 左滑删除按钮适配器
 * @author xuhao
 *
 */
public class SwipeAdp extends BaseAdapter {
    /**
     * �����Ķ���
     */
	
    private Context mContext = null;
    private List<WXMessage> data;//listview中要显示的数据
    
    private int mRightWidth = 0;//左部宽度
    
    private int flag = -1;
    /**
     * @param mainActivity
     */
    public SwipeAdp(Context ctx,List<WXMessage> data, int rightWidth) {
        mContext = ctx;
        this.data = data;
        mRightWidth = rightWidth;
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    /**
     * 初始化listview item
     * position：listview中item的位置
     */
    public View getView(final int position, View convertView, ViewGroup parent) {
    	
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();
            holder.item_left = (RelativeLayout)convertView.findViewById(R.id.item_left);
            holder.item_right = (RelativeLayout)convertView.findViewById(R.id.item_right);
            
            holder.tv_title = (TextView)convertView.findViewById(R.id.tv_title);
            
            holder.item_right_txt = (TextView)convertView.findViewById(R.id.item_right_txt);
            holder.item_clock = (TextView)convertView.findViewById(R.id.item_clock);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        
        LinearLayout.LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        holder.item_left.setLayoutParams(lp1);
        LinearLayout.LayoutParams lp2 = new LayoutParams(mRightWidth, LayoutParams.MATCH_PARENT);
        holder.item_right.setLayoutParams(lp2);
        
        WXMessage msg = data.get(position);
        
        holder.tv_title.setText(msg.getTitle());
        int alarm_time = Integer.parseInt(msg.getCourse_alarm());
        if(alarm_time < 0){
        	holder.item_clock.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.clock));
        }else{
        	holder.item_clock.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.clocked));
        }
        //删除事件
        holder.item_right_txt.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mListener != null) {
                	flag = 1;
                    mListener.onRightItemClick(flag, v, position);
                }
            }
        });
        //提醒事件
        holder.item_clock.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (mListener != null) {
                	flag = 0;
                    mListener.onRightItemClick(flag, v, position);
                    
                }
            }
        });
        
        return convertView;
    }

    static class ViewHolder {
    	RelativeLayout item_left; //listview中每一行左边要显示的部分
    	RelativeLayout item_right;//listview中每一行右边要显示的部分

    	/** 左边布局中显示一个listview*/
        TextView tv_title;
//        TextView tv_msg;
//        TextView tv_time;
//        ImageView iv_icon;
        /** 右边布局中显示一个两个图片按钮的文字部分*/
        TextView item_right_txt;
        TextView item_clock;
    }
    
    private onRightItemClickListener mListener = null;
    
    public void setOnRightItemClickListener(onRightItemClickListener listener){
    	mListener = listener;
    }
    /**
     * 声明回调函数，在用到该适配器的类中需要对该接口进行实现，从而完成对图片按钮的响应事件
     * @author xuhao
     *
     */
    public interface onRightItemClickListener {
        void onRightItemClick(int flag, View v, int position);
    }
}
